package com.dnjn.happiness.emo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.common.config.ext.SpringContextHolder;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.emo.api.domain.UserHcmQxPtdata;
import com.dnjn.happiness.emo.api.domain.UserHcmQxPtdataBean;
import com.dnjn.happiness.emo.mapper.UserHcmQxPtdataMapper;
import com.dnjn.happiness.emo.service.IUserHcmQxPtdataService;
import com.dnjn.happiness.emo.webapi.hcm.HCM_SAPHRClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * 考勤记录userHcmQxPtdataService业务层处理
 * 
 * @author amber
 * @date 2024-03-13
 */
@Service
public class UserHcmQxPtdataServiceImpl extends ServiceImpl<UserHcmQxPtdataMapper, UserHcmQxPtdata>  implements IUserHcmQxPtdataService
{
    private static final Logger log = LoggerFactory.getLogger(UserHcmQxPtdataServiceImpl.class);

    @Override
    public void getYesterdayUserHcmQxPtdataRecord() {
        String host = getEnv("hcm.host");;
        final LocalDate startDate = LocalDate.now().minusDays(1);
        final LocalDate endDate = LocalDate.now();

        String resp = null;
        try {
            String userHcmQxPtdataKey = getEnv("hcm.key");
            String userHcmQxPtdataSercet = getEnv("hcm.sercet");

            resp = HCM_SAPHRClient.loadQxPtData(host, startDate, endDate,userHcmQxPtdataKey,userHcmQxPtdataSercet);
            log.info("消费接口信息返回resp===========" + resp);
            UserHcmQxPtdataBean bean = JSON.parseObject(resp, new TypeReference<UserHcmQxPtdataBean>() {});
            if (!CollectionUtils.isEmpty(bean.getT_QJ())){
                for (UserHcmQxPtdataBean.TQJ tqj : bean.getT_QJ()) {
                    UserHcmQxPtdata dto = new UserHcmQxPtdata();
                    dto.setEmpno(tqj.getZWORKCODE())
                            .setDataType(Constants.TQJ)
                            .setPersonNum(tqj.getPersonNum())
                            .setAwart(tqj.getAwart())
                            .setAtext(tqj.getAtext())
                            .setStartDate(tqj.getStartDate())
                            .setEndDate(tqj.getEndDate())
                            .setBeginTime(tqj.getBeginTime())
                            .setEndTime(tqj.getEndTime())
                            .setLeaveTime(tqj.getLeaveTime());
                    baseMapper.insert(dto);
                }
            }
            if (!CollectionUtils.isEmpty(bean.getT_YC())){
                for (UserHcmQxPtdataBean.TYC tyc : bean.getT_YC()) {
                    UserHcmQxPtdata dto = new UserHcmQxPtdata();
                    dto.setEmpno(tyc.getEmpno())
                            .setDataType(Constants.TYC)
                            .setPersonNum(tyc.getPersonNum())
                            .setCheckDate(tyc.getCheckDate())
                            .setBelateCount(tyc.getBelateCount())
                            .setEarlyCount(tyc.getEarlyCount())
                            .setAbsenceCount(tyc.getAbsenceCount())
                            .setMakeupCount(tyc.getMakeupCount());
                    baseMapper.insert(dto);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("查询考勤接口记录失败：" + e);
        }
    }

    @Override
    public List<UserHcmQxPtdata> selectAttendance(Long userId, String time) {
        return baseMapper.selectAttendance(userId,time);
    }
    private static String getEnv(String key) {
        try {
            return SpringContextHolder.getContext().getEnvironment().getProperty(key);
        } catch (Exception e) {
            log.warn(String.format("[warn] %s %s; config [%s] first!", e.getMessage(), e.getClass().getCanonicalName(), key));
        }
        return null;

    }
}
